package com.shiyanlou.lesson10.serviceImpl;

import java.util.List;

import com.shiyanlou.lesson10.domain.Eat;
import com.shiyanlou.lesson10.mapper.EatMapper;
import com.shiyanlou.lesson10.service.EatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shiyanlou.lesson10.domain.Food;
import com.shiyanlou.lesson10.domain.PaginationObject;
import com.shiyanlou.lesson10.mapper.FoodMapper;
import com.shiyanlou.lesson10.service.FoodService;

@Service
public class EatServiceImpl implements EatService {

    @Autowired
    private EatMapper foodMapper;

    public Eat getFoodById(int id) {
        Eat food = foodMapper.getById(id);
        return food;
    }

    public int insertFood(Eat food) {
        int modifyId = foodMapper.insert(food);
        return modifyId;
    }

    public PaginationObject getAllFood(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Eat> foods = foodMapper.getAll();
        PageInfo<Eat> appsPageInfo = new PageInfo<Eat>(foods);
        long total = appsPageInfo.getTotal();
        PaginationObject paginationObject = new PaginationObject(foods, pageNum, pageSize, total);
        return paginationObject;
    }

    public int updateFood(Eat food) {
        int modifyId = foodMapper.update(food);
        return modifyId;
    }

    public int deleteFood(int id) {
        int modifyId = foodMapper.delete(id);
        return modifyId;
    }
}
